{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "989e9e16",
   "metadata": {},
   "source": [
    "- 记录人：程方圆\n",
    "- 时间：2021-11-25\n",
    "- week13\n",
    "- 线上学习资源：\n",
    "> 1. [python官方文档]\n",
    "> 2. [智超gitee]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd79b541",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "## Json(文本数据)--->Dict(python中的字典数据)\n",
    "## 字典和列表的综合实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7b1f480",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "## [集合](https://docs.python.org/3/tutorial/datastructures.html#sets)\n",
    "> 1. 集合的性质\n",
    ">> 1. 无序的(unordered)\n",
    ">> 2. 不重复的(no duplicate)\n",
    "> 2. 集合的一般方法\n",
    ">> 1. 增加\n",
    ">> 2. 删除\n",
    ">> 3. 查询：in 查询成员关系\n",
    "> 3. 集合的数学逻辑运算\n",
    "> 1.集合的性质及概念\n",
    "- 思考：我想了解一组数据中有哪些唯一存在的值？(假如是列表)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "941a3fc7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T00:57:28.419492Z",
     "start_time": "2021-12-22T00:57:28.410475Z"
    }
   },
   "outputs": [],
   "source": [
    "no_duplicate_age = []\n",
    "list_age = [18,24,25,24,18,23,18]\n",
    "for i in list_age:\n",
    "    if i in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e217b7bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T00:57:35.739232Z",
     "start_time": "2021-12-22T00:57:35.716154Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "no_duplicate_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b619457b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-25T02:19:26.600268Z",
     "start_time": "2021-11-25T02:19:26.584648Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{24, 25, 18, 23}\n"
     ]
    }
   ],
   "source": [
    "# list--->set 的方法：set()\n",
    "no_duplicate_age_set = set(list_age)\n",
    "print(no_duplicate_age_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2c35c17d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T00:59:26.270643Z",
     "start_time": "2021-12-22T00:59:26.256575Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'banana', 'orange', 'pear'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方文档测试代码\n",
    "# 1. 移除重复项\n",
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket)\n",
    "# 2. in 查询成员关系\n",
    "print('orange'in basket)\n",
    "print('crabgrass'in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fc975f3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-25T02:28:24.119439Z",
     "start_time": "2021-11-25T02:28:24.105852Z"
    }
   },
   "source": [
    "# 2. 集合的一般方法\n",
    "- 集合的创建的两种一般方法：\n",
    "    - 1. set(list)\n",
    "    - 2. 直接按照集合的结构创建：{valu1,value2,value3,...}\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84e02b7a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T00:59:30.080805Z",
     "start_time": "2021-12-22T00:59:30.075819Z"
    }
   },
   "source": [
    "> 1. 增加add()\n",
    ">> 1. 结果 ：Add an element to a set\n",
    ">> 2. 注意：This has no effect if the element is already present."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4116fefd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:00:05.747350Z",
     "start_time": "2021-12-22T01:00:05.740359Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5ecbb845",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:00:07.607717Z",
     "start_time": "2021-12-22T01:00:07.598716Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function add:\n",
      "\n",
      "add(...) method of builtins.set instance\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "419546f8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:00:09.943874Z",
     "start_time": "2021-12-22T01:00:09.924831Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(99)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2c7b0477",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:00:38.174437Z",
     "start_time": "2021-12-22T01:00:38.157635Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eed5eb40",
   "metadata": {},
   "source": [
    "> 2. 删除\n",
    ">> 1. pop():Remove and return an arbitrary set element.&Raises KeyError if the set is empty\n",
    ">> 2. remove()\n",
    ">> 3. discard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "aa9862e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:00:58.668891Z",
     "start_time": "2021-12-22T01:00:58.650677Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e39b2079",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:00:59.963468Z",
     "start_time": "2021-12-22T01:00:59.959467Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.set instance\n",
      "    Remove and return an arbitrary set element.\n",
      "    Raises KeyError if the set is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "29e445aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:01:01.164247Z",
     "start_time": "2021-12-22T01:01:01.157246Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n",
      "{1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.Remove and return an arbitrary set element.\n",
    "print (set_1)\n",
    "set_1.pop()\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a898af7b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:02:01.025149Z",
     "start_time": "2021-12-22T01:02:01.010145Z"
    }
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'pop from an empty set'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-0defeaec728f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 2. Raises KeyError if the set is empty.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mset_2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mset_2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'pop from an empty set'"
     ]
    }
   ],
   "source": [
    "# 2. Raises KeyError if the set is empty.\n",
    "set_2 = set([])\n",
    "set_2.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f099bf10",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:01:43.214368Z",
     "start_time": "2021-12-22T01:01:43.196354Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ef06d8c4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:02:16.027751Z",
     "start_time": "2021-12-22T01:02:16.018730Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# Remove an element from a set;it must be a member.\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "3dbce8b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:02:44.096907Z",
     "start_time": "2021-12-22T01:02:44.091416Z"
    }
   },
   "outputs": [],
   "source": [
    "# 2. If the element is not a member, raise a KeyError. 一般项目中 remove（）会和 in 共同使用\n",
    "set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "17674e6f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:02:51.165014Z",
     "start_time": "2021-12-22T01:02:51.158015Z"
    }
   },
   "outputs": [],
   "source": [
    "# - 避免KeyError的方式：\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fca624fd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:02:51.836222Z",
     "start_time": "2021-12-22T01:02:51.818926Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function discard:\n",
      "\n",
      "discard(...) method of builtins.set instance\n",
      "    Remove an element from a set if it is a member.\n",
      "    \n",
      "    If the element is not a member, do nothing.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.discard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6beaa5d9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:02:56.261872Z",
     "start_time": "2021-12-22T01:02:56.248869Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b6c6d02b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:03:05.995599Z",
     "start_time": "2021-12-22T01:03:05.981568Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9456d000",
   "metadata": {},
   "source": [
    "### 3. 集合的运算\n",
    "> 1. 集合的交集\n",
    "> 2. 集合的并集\n",
    "> 3. 集合的差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "09aa27dd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:05:49.750423Z",
     "start_time": "2021-12-22T01:05:49.737387Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,2,5,6,7,2,3,2,4,4])\n",
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e4fb505",
   "metadata": {},
   "source": [
    "> 1. 集合的交集\n",
    ">> 1. 集合的使用方法:set_1.intersection(set_2)\n",
    ">> 2. 使用逻辑运算符：set_1 & set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e5b12a1a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:05:52.047179Z",
     "start_time": "2021-12-22T01:05:52.032842Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function intersection:\n",
      "\n",
      "intersection(...) method of builtins.set instance\n",
      "    Return the intersection of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in both sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.intersection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "dfaa1eaa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:07:20.522806Z",
     "start_time": "2021-12-22T01:07:20.506297Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用集合的方法：set_1.intersection(set_2)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1.intersection(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "483cacf0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:07:34.503399Z",
     "start_time": "2021-12-22T01:07:34.494400Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用逻辑运算符：set_1 & set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 & set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "044a5036",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:07:48.054588Z",
     "start_time": "2021-12-22T01:07:48.036563Z"
    }
   },
   "outputs": [],
   "source": [
    "# 实践：\n",
    "set_1_xu = {\"星期一12-14\",\"星期二3-5\",\"星期二12-14\" }\n",
    "set_2_前端 = {\"星期二3-5\",\"星期二12-14\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8d500de3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:07:57.565877Z",
     "start_time": "2021-12-22T01:07:57.546791Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'星期二12-14', '星期二3-5'}\n",
      "结论：周一班 API课 让给前端的同学~\n"
     ]
    }
   ],
   "source": [
    "print(set_1_xu & set_2_前端)\n",
    "print(\"结论：周一班 API课 让给前端的同学~\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9addc7c0",
   "metadata": {},
   "source": [
    "> 2.集合的并集\n",
    ">> 1. 使用并集的方法：set_1.union(set_2)\n",
    ">> 2. 使用逻辑运算符：set_1 | set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a66b7b91",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:09:57.439293Z",
     "start_time": "2021-12-22T01:09:57.425292Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用并集的方法：set_1.union(set_2)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1.union(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4a76e453",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:10:00.127505Z",
     "start_time": "2021-12-22T01:10:00.115501Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 2. 使用逻辑运算符：set_1 | set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e6c0e80a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:10:02.039923Z",
     "start_time": "2021-12-22T01:10:02.022918Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的时间 :  1640135402.0249212\n",
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "结束的时间 :  1640135402.025921\n",
      "运行的时间 :  0.0009999275207519531\n"
     ]
    }
   ],
   "source": [
    "# 计算运行效率 = 运行后的时间节点 - 运行前的时间节点  : 思考并对比列表运算的执行效率 \n",
    "import time\n",
    "start_time = time.time()\n",
    "print(\"开始的时间 : \",start_time)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)\n",
    "stop_time = time.time()\n",
    "print(\"结束的时间 : \",stop_time)\n",
    "print(\"运行的时间 : \", stop_time - start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66e86486",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-25T03:43:27.265128Z",
     "start_time": "2021-11-25T03:43:27.251003Z"
    }
   },
   "source": [
    "> 3. 集合的差集\n",
    ">> 1. 使用差集的方法：set_1.difference(set_2)\n",
    ">> 2. 使用逻辑运算符：set_1 - set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a993c8c6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:10:20.915689Z",
     "start_time": "2021-12-22T01:10:20.904598Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1 和 set_2 差集结果 :  {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1.difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "1c50eaa2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:11:52.486508Z",
     "start_time": "2021-12-22T01:11:52.478510Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_2 和 set_1 差集结果 :  {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2.difference(set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "8dd50540",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:11:53.026431Z",
     "start_time": "2021-12-22T01:11:53.018367Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1 和 set_2 差集结果 :  {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1 - set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f5036848",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:11:53.516519Z",
     "start_time": "2021-12-22T01:11:53.506415Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_2 和 set_1 差集结果 :  {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2 - set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "238acf1d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:11:55.370760Z",
     "start_time": "2021-12-22T01:11:55.354486Z"
    }
   },
   "source": [
    "> 4. 对称差集\n",
    ">> 1. 使用对称差集的方法：set_1.symmetric_difference(set_2)\n",
    ">> 2. 使用逻辑运算符：？(set_1 - set_2) | (set_2-set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "4171acdb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:12:18.266679Z",
     "start_time": "2021-12-22T01:12:18.248273Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function symmetric_difference:\n",
      "\n",
      "symmetric_difference(...) method of builtins.set instance\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.symmetric_difference)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "dab74daf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:12:52.035542Z",
     "start_time": "2021-12-22T01:12:52.016423Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果 :  {0, 66, 2, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",set_1.symmetric_difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "b74083f3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-22T01:13:05.375938Z",
     "start_time": "2021-12-22T01:13:05.363813Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果 :  {0, 2, 66, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",(set_1 - set_2) | (set_2-set_1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfa8a2a8",
   "metadata": {},
   "source": [
    "### 集合实践：（P129）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d90abff6",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2021-12-22T01:13:43.463Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vowels :  {'i', 'u', 'e', 'a', 'o'}\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "print(\"vowels : \",vowels)\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "print(\"word : \",set(word))\n",
    "found = vowels.intersection(set(word))\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39b6611f",
   "metadata": {},
   "source": [
    "# 课后作业\n",
    "> 1. 课堂练习\n",
    "> 2. 课本第三章 课后项目的练习\n",
    "> 3. 总结集合的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6c6cf87",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
